package io.renren.modules.feedback.controller;

import java.util.*;

import io.renren.common.annotation.SysLog;
import io.renren.common.utils.ConstantUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.modules.feedback.entity.FeedbackinfosCustomEntity;
import io.renren.modules.projectinfo.service.ProjectinfosService;
import io.renren.modules.sys.controller.AbstractController;
import io.renren.modules.user.service.UserinfosService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.feedback.entity.FeedbackinfosEntity;
import io.renren.modules.feedback.service.FeedbackinfosService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;




/**
 * 
 * 
 * @author »ÆÇå
 * @email 
 * @date 2018-10-23 12:42:34
 */
@RestController
@RequestMapping("/feedback/feedbackinfos")
public class FeedbackinfosController extends AbstractController {
	@Autowired
	private FeedbackinfosService feedbackinfosService;

	@Autowired
	private ProjectinfosService projectinfosService;

	@Autowired
	private UserinfosService userinfosService;


	/**
	 * 获取审核反馈列表
	 */
	@SysLog("查询审核反馈列表")
	@RequestMapping("/auditlist")
	@RequiresPermissions("feedback:feedbackinfos:auditlist")
	public R auditList(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<FeedbackinfosCustomEntity> feedbackinfosList = feedbackinfosService.queryAuditFeedbacks(query);
		int total = feedbackinfosService.queryAuditTotal(query);
		PageUtils pageUtil = new PageUtils(feedbackinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}


	/**
	 * 获取反馈信息
	 */
	@SysLog("查询反馈信息")
	@GetMapping("/auditinfo/{feedbackinfoid}")
	@RequiresPermissions("feedback:feedbackinfos:auditinfo")
	public R auditInfo(@PathVariable("feedbackinfoid")String feedbackinfoid){
		FeedbackinfosCustomEntity feedbackinfos = feedbackinfosService.queryAuditInfo(feedbackinfoid);
		return R.ok().put("feedbackinfos", feedbackinfos);
	}

	/**
	 * 审核反馈信息
	 */
	@SysLog("审核反馈信息")
	@PostMapping("/audit")
	@RequiresPermissions("feedback:feedbackinfos:audit")
	public R audit(@RequestBody FeedbackinfosEntity feedbackinfos){
		Integer auditstatus = feedbackinfos.getAuditstatus();
		//依旧是待审核，直接返回
		if(auditstatus == ConstantUtils.AUDIT){
			return R.ok();
		}
		//审核验证
		R auditError = feedbackinfosService.judgeFeedbackAuditError(feedbackinfos, getUserId());
		if(auditError != null){
			return auditError;
		}
		feedbackinfosService.audit(feedbackinfos);
		return R.ok();
	}


	/**
	 * 反馈列表
	 */
	@SysLog("查询反馈列表")
	@RequestMapping("/list")
	@RequiresPermissions("feedback:feedbackinfos:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<FeedbackinfosCustomEntity> feedbackinfosList = feedbackinfosService.queryCustomList(query);
        int total = feedbackinfosService.queryTotalById(query);
		PageUtils pageUtil = new PageUtils(feedbackinfosList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@SysLog("查询反馈信息")
	@GetMapping("/info/{feedbackinfoid}")
	@RequiresPermissions("feedback:feedbackinfos:info")
	public R info(@PathVariable("feedbackinfoid")String feedbackinfoid){
		FeedbackinfosCustomEntity feedbackinfos = feedbackinfosService.queryCustomInfo(feedbackinfoid);
		return R.ok().put("feedbackinfos", feedbackinfos);
	}
	
	/**
	 * 保存
	 */
	@SysLog("保存反馈信息")
	@PostMapping("/save")
	@RequiresPermissions("feedback:feedbackinfos:save")
	public R save(@RequestBody FeedbackinfosEntity feedbackinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(feedbackinfos, AddGroup.class);
		//是否阅读
		R isReadError = feedbackinfosService.judgeFeedbackIsReadError(feedbackinfos);
		if(isReadError != null){
			return isReadError;
		}
		//反馈类型验证，项目验证
		R typeError = feedbackinfosService.judgeFeedbackTypeError(feedbackinfos);
		if(typeError != null){
			return typeError;
		}
		//用户验证
		R userError = userinfosService.judgeUserError(feedbackinfos.getFeedbackuser());
		if (userError != null){
			return userError;
		}
		//项目需要时审核通过的才能保存
		if(feedbackinfos.getFeedbacktype().equals(ConstantUtils.PROJECTFEEDBACK)){
			R projectAuditeSucceed = projectinfosService.judgeProjectAuditSucceed(feedbackinfos.getTargetid());
			if(projectAuditeSucceed != null){
				return projectAuditeSucceed;
			}
		}
		//审核验证
		R feedbackAuditStatusError = feedbackinfosService.judgeFeedbackAuditError(feedbackinfos, getUserId());
		if(feedbackAuditStatusError != null){
			return feedbackAuditStatusError;
		}
		feedbackinfosService.save(feedbackinfos);
		return R.ok();
	}



	/**
	 * 修改
	 */
	@SysLog("修改反馈信息")
	@PostMapping("/update")
	@RequiresPermissions("feedback:feedbackinfos:update")
	public R update(@RequestBody FeedbackinfosEntity feedbackinfos){
		//添加pojo属性验证
		ValidatorUtils.validateEntity(feedbackinfos, AddGroup.class);
		//是否阅读
		R isReadError = feedbackinfosService.judgeFeedbackIsReadError(feedbackinfos);
		if(isReadError != null){
			return isReadError;
		}
		//反馈类型验证，项目验证
		R typeError = feedbackinfosService.judgeFeedbackTypeError(feedbackinfos);
		if(typeError != null){
			return typeError;
		}
		//用户存在验证
		R userError = userinfosService.judgeUserError(feedbackinfos.getFeedbackuser());
		if (userError != null){
			return userError;
		}
		//项目审核通过验证
		if(feedbackinfos.getFeedbacktype().equals(ConstantUtils.PROJECTFEEDBACK)){
			R projectAuditeSucceed = projectinfosService.judgeProjectAuditSucceed(feedbackinfos.getTargetid());
			if(projectAuditeSucceed != null){
				return projectAuditeSucceed;
			}
		}
		//审核验证
		R auditError = feedbackinfosService.judgeFeedbackAuditError(feedbackinfos, getUserId());
		if(auditError != null){
			return auditError;
		}
		feedbackinfosService.update(feedbackinfos);
		return R.ok();
	}


	
	/**
	 * 删除
	 */
	@SysLog("删除反馈信息")
	@PostMapping("/delete")
	@RequiresPermissions("feedback:feedbackinfos:delete")
	public R delete(@RequestBody String[] feedbackinfoids){
		feedbackinfosService.deleteBatch(feedbackinfoids);
		return R.ok();
	}
	
}
